package com.spark.tags;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

/**
 * 按钮组件.
 * 
 * @version $Id: Button.java v 1.0 2010-12-24 下午01:23:03 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class XButton extends ViewComponent {
	
	public static final String TAG_NAME = "xbutton";
	
	public static final String PROPERTITY_TEXT = "text";
	public static final String PROPERTITY_ICON = "icon";
	public static final String PROPERTITY_ICON_HEIGHT = "iconHeight";
	public static final String PROPERTITY_TEXT_LAYOUT = "textLayout";
	public static final String PROPERTITY_BUTTON_STYLE = "btnStyle";
	public static final String PROPERTITY_TEXT_STYLE = "textStyle";
	public static final String PROPERTITY_TEXT_CLASS = "textClass";
	public static final String PROPERTITY_CLICK = "click";
	public static final String PROPERTITY_TITLE = "title";
	
	
	protected static final String BUTTON_WIDTH = "btnWidth";
	protected static final String BUTTON_HEIGHT = "btnHeight";
	protected static final String KEY_BTN_HTML = "btnHtml";
	
	private static final int DEFAULT_HEIGHT = 24;
	private static final int DEFAULT_WIDTH = 60;
	
	private static final String DEFAULT_TEMPLATES = "xbutton.ftl";
	private static final String DEFAULT_BUTTON_CLASSNAME = "s_btn";
	
	protected int getDefaultWidth(){
		return DEFAULT_WIDTH;
	}
	
	protected int getDefaultHeight(){
		return DEFAULT_HEIGHT;
	}
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		String className = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(className);
		if(!"".equals(className.trim()))sb.append(" ");
		sb.append(DEFAULT_BUTTON_CLASSNAME);
		return sb.toString();
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		String clickHandler = view.getString(PROPERTITY_CLICK, "");
		if(!"".equals(clickHandler)){
			boolean hasClick = false;
			XMap events = view.getChild(PROPERTITY_EVENTS);
			if(events == null){
				events = new XMap(DEFAULT_TAG_PREFIX,DEFAULT_NAME_SPACE,PROPERTITY_EVENTS);
				view.addChild(events);
			}else{
				List<XMap> list = events.getChilds();
				if(list != null){
					Iterator<XMap> it = list.iterator();
					while(it.hasNext()){
						XMap event = it.next();
						String eventName = event.getString(PROPERTITY_EVENT_NAME, "");
						if(PROPERTITY_CLICK.equals(eventName)) {
							hasClick = true;
							break;
						}
					}
				}
			}
			if(!hasClick) {
				XMap event = new XMap(DEFAULT_TAG_PREFIX,DEFAULT_NAME_SPACE,PROPERTITY_EVENT);
				event.putString(PROPERTITY_EVENT_NAME, PROPERTITY_CLICK);
				event.putString(PROPERTITY_EVENT_HANDLER, clickHandler);
				events.addChild(event);
			}
		}
		super.init(view, context);
		
		Integer height = (Integer)context.get(PROPERTITY_HEIGHT);
		height -= 4;
		context.put(BUTTON_HEIGHT, height);
		Integer width = (Integer)context.get(PROPERTITY_WIDTH);
		width -= 6;
		context.put(BUTTON_WIDTH, width);
		String text = view.getString(PROPERTITY_TEXT, "");//TODO:多语言!

		
		String icon = view.getString(PROPERTITY_ICON, "");
		String textstyle = view.getString(PROPERTITY_TEXT_STYLE, "");
		String html = "";
		if(!"".equals(icon)){// || "null".equalsIgnoreCase(icon)
			html = creatIconButtonHtml(context,view,icon,text,textstyle);
		}else{
			html = creatButtonHtml(context,view,text,textstyle);
		}
		
		context.put(KEY_BTN_HTML, html);
		context.put(PROPERTITY_TEXT, text);
		context.put(PROPERTITY_TITLE, view.getString(PROPERTITY_TITLE, ""));
		String btnstyle = view.getString(PROPERTITY_BUTTON_STYLE, "");
		context.put(PROPERTITY_BUTTON_STYLE, btnstyle);
	}
	
	protected void filterJsonConfig(SparkContext context){
		context.removeJsonConfig(PROPERTITY_WIDTH);
		context.removeJsonConfig(PROPERTITY_HEIGHT);
	}
	
	private String creatIconButtonHtml(SparkContext context, XMap view,String icon,String text,String textstyle){
		String layout = view.getString(PROPERTITY_TEXT_LAYOUT, "right");
		String textClass = view.getString(PROPERTITY_TEXT_CLASS,"");
		StringBuilder sb = new StringBuilder();
		if("bottom".equalsIgnoreCase(layout)) {
			sb.append("<div");
			if(!"".equals(textClass)) sb.append(" class=\"").append(textClass).append("\"");
			sb.append(" style=\"height:");
			sb.append(view.getInt(PROPERTITY_ICON_HEIGHT, 16));
			sb.append("px;width:100%;");
			if(!"".equalsIgnoreCase(icon)){
				sb.append("background:url(");
				sb.append(icon);
				sb.append(") no-repeat 50% 50%;");
			}
			sb.append("\"></div>");
			if(!"".equals(text)) {
				sb.append("<div");
				if(!"".equals(textClass)) sb.append(" class=\"").append(textClass).append("\"");
				sb.append(" style=\"width:100%;");
				sb.append(textstyle);
				sb.append("padding-top:3px;");
				sb.append("\">");
				sb.append(text);
				sb.append("</div>");
			}
		}else {
			String btnstyle = view.getString(PROPERTITY_BUTTON_STYLE, "");
			sb.append("<span");
			if(!"".equals(textClass)) sb.append(" class=\"").append(textClass).append("\"");
			sb.append(" style=\"padding-left:20px;");//padding-left:3px;
			Integer btnHeight = (Integer)context.get(BUTTON_HEIGHT);
			sb.append("height:").append(btnHeight).append("px;");
			sb.append("line-height:").append(btnHeight).append("px;");
			sb.append("background:url(").append(icon).append(") no-repeat 0px 50%;");
			sb.append(textstyle);
			sb.append("\">");
			sb.append("".equals(text) ? "&#160;" : text);
			sb.append("</span>");
			view.put(PROPERTITY_BUTTON_STYLE, btnstyle);
		}
		return sb.toString();
		
	}
	
	private String creatButtonHtml(SparkContext context,XMap view,String text,String textstyle){
		String textClass = view.getString(PROPERTITY_TEXT_CLASS,"");
		StringBuilder sb = new StringBuilder();
		sb.append("<div");
		if(!"".equals(textClass)) sb.append(" class=\"").append(textClass).append("\"");
		sb.append(" style=\"");
		Integer btnHeight = (Integer)context.get(BUTTON_HEIGHT);
		sb.append("height:").append(btnHeight).append("px;");
		sb.append("line-height:").append(btnHeight).append("px;");
		sb.append(textstyle);
		sb.append("\">");
		sb.append("".equals(text) ? "&#160;" : text);
		sb.append("</div>");
		return sb.toString();
	}
	
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<TABLE class=\"").append(get(map,"className")).append("\" style=\"").append(get(map,"style")).append(";\" id=\"").append(get(map,"id")).append("\" cellSpacing=\"0\">\r");
		sb.append("    <TBODY>\r");
		sb.append("        <TR>\r");
		sb.append("            <TD class=\"s_btn-tl\"><I></I></TD>\r");
		sb.append("            <TD class=\"s_btn-tc\"></TD>\r");
		sb.append("            <TD class=\"s_btn-tr\"><I></I></TD>\r");
		sb.append("        </TR>\r");
		sb.append("        <TR>\r");
		sb.append("            <TD class=\"s_btn-ml\"><I></I></TD>\r");
		sb.append("            <TD class=\"s_btn-mc\">\r");
		sb.append("                <BUTTON type=\"button\" hidefocus=\"true\" stype=\"btn\" title=\"").append(get(map,"title")).append("\" style=\"width:").append(get(map,"btnWidth")).append("px;height:").append(get(map,"btnHeight")).append("px;").append(get(map,"btnStyle")).append("\">").append(get(map,"btnHtml")).append("</BUTTON>\r");
		sb.append("            </TD>\r");
		sb.append("            <TD class=\"s_btn-mr\"><I></I></TD>\r");
		sb.append("        </TR>\r");
		sb.append("        <TR>\r");
		sb.append("            <TD class=\"s_btn-bl\"><I></I></TD>\r");
		sb.append("            <TD class=\"s_btn-bc\"></TD>\r");
		sb.append("            <TD class=\"s_btn-br\"><I></I></TD>\r");
		sb.append("        </TR>\r");
		sb.append("    </TBODY>\r");
		sb.append("</TABLE>\r");
		sb.append("<script>new Spark.XButton(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}
}
